﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MrPP.Helper;
using Mirror;
using MrPP.Network;

namespace MrPP.SerriedCabinet
{
    public class SearchModel : NetworkBehaviour
    {
        [SyncVar(hook = "changeOpen")]
        public int isOpen = -1;
        public Action<int, int> onOpen{
            get;
            set;
        }
        public void changeOpen(int oldValue, int newValue) {
            onOpen?.Invoke(oldValue, newValue);
        }


        [SyncVar(hook = "changeSelected")]
        public int selected = -1;
        public Action<int, int> onSelected
        {
            get;
            set;
        }

        public void changeSelected(int oldValue, int newValue)
        {
            onSelected?.Invoke(oldValue, newValue);
           // selected = val;
        }

        [SyncVar(hook = "changeInput")]
        public string input = null;

        public Action<string, string> onInput
        {
            get;
            set;
        }
        public void changeInput(string oldValue, string newValue)
        {
            onInput?.Invoke(oldValue, newValue);
            //input = val;
        }
        public Action onRefresh
        {
            get;
            set;  
        }
        public class DataSyncList : SyncList<CabinetGroup.ItemData>{}
        public DataSyncList datas = new DataSyncList();

        SyncListCallback dataCallback_ = null;
        void changed(DataSyncList.Operation op, int index, CabinetGroup.ItemData oldItem, CabinetGroup.ItemData newItem)
        {

            if (dataCallback_ == null)
            {
                dataCallback_ = this.gameObject.AddComponent<SyncListCallback>();


                dataCallback_.start = delegate
                {
                    onRefresh?.Invoke();
                };
                dataCallback_.action = delegate
                {
                    onRefresh?.Invoke();
                };
            }
            dataCallback_.reset();


        }
        public void Awake() {
            datas.Callback += changed;
        }

    }
}